/* This file is part of the Pardus Economy Planner.

   The Pardus Economy Planner is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   The Pardus Economy Planner is distributed in the hope that it
   will be useful, but WITHOUT ANY WARRANTY; without even the implied
   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the Pardus Economy Planner.  If not, see
   <http://www.gnu.org/licenses/>.
*/

/* Export Protocol for Sectors
 * JSON Representation of the Sector.
 */

ExportModule = {
  ExportSector: function(sector)
  {
    var buildings = [];
    var planets = [];
    var starbases = [];
    var backgrounds = [];

    for (var i = 0; i < sector.Buildings.length; i++)
    {
      var building = sector.Buildings[i];
      buildings.push({
        "id": building.ID,
        "name": building.Name,
        "owner": building.Owner,
        "level": building.GetLevel()
      });
    }

    for (var i = 0; i < sector.Planets.length; i++)
    {
      var planet = sector.Planets[i];
      planets.push({
        "id": planet.ID,
        "type": planet.Type,
        "population": planet.GetPopulation()
      });
    }

    for (var i = 0; i < sector.Starbases.length; i++)
    {
      var starbase = sector.Starbases[i];
      starbases.push({
        "id": starbase.ID,
        "owner": (starbase.Npc? 225: starbase.Owner),
        "population": starbase.GetPopulation()
      });
    }

    for (var i = 0; i < sector.Backgrounds.length; i++)
    {
      var background = sector.Backgrounds[i];
      backgrounds.push({
        "id": background.ID,
        "image": background.Image
      });
    }

    return JSON.encode({
      "version": ProtocolVersion,
      "name": sector.Name,
      "map": sector.Map,
      "buildings": buildings,
      "planets": planets,
      "starbases": starbases,
      "backgrounds": backgrounds
    });
  },
  ExportPricelist: function(pricelist)
  {
    return JSON.encode({
      "version": ProtocolVersion,
      "name": pricelist.name,
      "buy": pricelist.buy,
      "sell": pricelist.sell
    });
  }
};